<?php

class Package extends Model {

	private $databaseService;
	private $viewAllStatement;
	private $saveStatement;
	private $deleteStatement;
	private $updateStatement;
	
	public function __construct($databaseService){
		parent::__construct();
		$this->databaseService = $databaseService;
		$this->viewAllStatement = parent::prepareStatement("SELECT dbuid, name, metric_system_unit_id, quantity FROM package_unit");
		$this->saveStatement = parent::prepareStatement("INSERT INTO package_unit (name, metric_system_unit_id, quantity) VALUES (?, ?, ?)");
		$this->deleteStatement = parent::prepareStatement("DELETE FROM package_unit WHERE dbuid = ?");
		$this->updateStatement = parent::prepareStatement("UPDATE package_unit SET name = ?, metric_system_unit_id = ?, quantity = ? WHERE dbuid = ?");
	}

	public function viewAll(){
		$statement = $this->viewAllStatement; 
		$columns = array("dbuid", "name", "metric_system_unit_id", "quantity");
		$modelClassName = "PackageModel";
		
		return $this->databaseService->executeViewAllStatement($statement, $columns, $modelClassName);
	}
	
	public function save($name, $metricSystemUnitId, $quantity){
		//TODO throw exception when units are null
		$statement = $this->saveStatement; 
		$valueTypes = "sii";
		$values = array($name, $metricSystemUnitId, $quantity);
		
		return $this->databaseService->save($statement, $valueTypes, $values);
	}
	
	public function delete($dbuid){
		//TODO throw exception when units are null
		$statement = $this->deleteStatement; 
		$valueTypes = "i";
		$values = array($dbuid);
		
		return $this->databaseService->delete($statement, $valueTypes, $values);
	}
	
	public function update($dbuid, $name, $metricSystemUnitId, $quantity){
		//TODO throw exception when units are null
		$statement = $this->updateStatement; 
		$valueTypes = "siii";
		$values = array($name, $metricSystemUnitId, $quantity, $dbuid);
		
		return $this->databaseService->save($statement, $valueTypes, $values);
	}
}

class PackageModel{
	private $dbuid;
	private $name;
	private $metricSystemUnit;
	private $quantity;
	
	public function __construct($dbuid, $name, $metricSystemUnit, $quantity){
		$this->dbuid = $dbuid;
		$this->name = $name;
		$this->metricSystemUnit = $metricSystemUnit;
		$this->quantity = $quantity;
	}
	
	public function dbuid(){
		return $this->dbuid;
	}
	
	public function name(){
		return $this->name;
	}
	
	public function metricSystemUnit(){
		return $this->metricSystemUnit;
	}
	
	public function quantity(){
		return $this->quantity;
	}
}

/*
function viewAll(){
	$statement = $this->viewAllStatement; 
	
	$statement->execute();
	$statement->store_result();
	
	$column = array("dbuid", "name", "metric_system_unit_id", "quantity") ; 
	
	$data = array() ; // Array that accepts the data. 
	$params = array() ; // Parameter array passed to 'bind_result()' 
	foreach($column as $col_name) 
	{ 
	  // Assign the fetched value to the variable '$data[$name]' 
	  $params[] =& $data[$col_name] ; 
	} 
	$res = call_user_func_array(array($statement, "bind_result"), $params) ; 
	if(! $res) 
	{ 
	  echo "bind_result() failed: " . $mysqli->error . "\n" ; 
	} 
	else 
	{ 
	  $res = $statement->fetch() ; 
	  if($res) 
	  { 
		  echo "<pre>" . htmlentities(print_r($data, true)) . "</pre>\n" ; 
	  } 
	  else 
	  { 
		echo ((false !== $res) ? "End of data" : $statement->error) . "\n" ; 
	  } 
	} 
	$statement->close() ; 
*/